<?php


namespace catchAdmin\permissions\controller;


use catchAdmin\permissions\model\PortsConfig;
use catchAdmin\permissions\request\CreatePortsRequest;
use catcher\base\CatchController;
use catcher\base\CatchRequest as Request;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\Utils;

class Port extends CatchController
{
    protected $port_config;

    public function __construct(PortsConfig $ports_config)
    {
        $this->ports_config = $ports_config;
    }

    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function portList()
    {
        return CatchResponse::paginate($this->ports_config->getList());
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function portConfig()
    {
        return CatchResponse::success($this->ports_config->getAllList(),'查询成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function createPort(CreatePortsRequest $request)
    {
        $params = $request->param();
        $this->ports_config->storeBy($params);
        return CatchResponse::success('', '新建成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function updatePort(Request $request)
    {
        $params = $request->filterEmptyField()->param();
        $id = $params['id'];
        $this->ports_config->updateBy($id, $params);
        return CatchResponse::success('', '编辑成功');
    }
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
    public function delPort(Request $request)
    {
        $id = $request->post('id');
        $ids = Utils::stringToArrayBy($id);
        foreach ($ids as $_id) {
            $this->ports_config->deleteBy($_id);
        }
        return CatchResponse::success('', '删除成功');
    }
}